import requests
import json
import time

# API基础URL
BASE_URL = "http://localhost:8888/api"

def test_write_drill_config():
    """测试写入钻机配置接口"""
    print("测试写入钻机配置接口...")
    
    # 准备测试数据
    config_data = {
        "d": 12.5,           # 直径，单位：米
        "kn": 0.29,          # 螺杆转速系数
        "kt": 9.8,           # 螺杆扭矩系数
        "delta_p": 585,      # 压力
        "create_time": time.strftime("%Y%m%d%H%M%S"),  # 当前时间
        "status": 1          # 状态
    }
    
    try:
        response = requests.post(
            f"{BASE_URL}/drill-config/write",
            json=config_data,
            headers={"Content-Type": "application/json"}
        )
        
        print(f"状态码: {response.status_code}")
        print(f"响应内容: {response.text}")
        
        if response.status_code == 200:
            print("✅ 写入配置成功")
            return True
        else:
            print("❌ 写入配置失败")
            return False
            
    except Exception as e:
        print(f"❌ 请求出错: {e}")
        return False

def test_get_drill_config():
    """测试获取最新钻机配置接口"""
    print("\n测试获取最新钻机配置接口...")
    
    try:
        response = requests.get(f"{BASE_URL}/drill-config")
        
        print(f"状态码: {response.status_code}")
        
        if response.status_code == 200:
            config_data = response.json()
            print(f"获取到的配置: {json.dumps(config_data, indent=2, ensure_ascii=False)}")
            print("✅ 获取配置成功")
            return True
        else:
            print(f"❌ 获取配置失败: {response.text}")
            return False
            
    except Exception as e:
        print(f"❌ 请求出错: {e}")
        return False

def test_with_invalid_data():
    """测试写入无效数据"""
    print("\n测试写入无效数据...")
    
    # 准备无效的测试数据 (直径为负数)
    invalid_config_data = {
        "d": -5.0,           # 无效：直径为负数
        "kn": 0.29,
        "kt": 9.8,
        "delta_p": 585,
        "create_time": time.strftime("%Y%m%d%H%M%S"),
        "status": 1
    }
    
    try:
        response = requests.post(
            f"{BASE_URL}/drill-config/write",
            json=invalid_config_data,
            headers={"Content-Type": "application/json"}
        )
        
        print(f"状态码: {response.status_code}")
        print(f"响应内容: {response.text}")
        
        if response.status_code == 400 or response.status_code == 500:
            print("✅ 正确拒绝了无效数据")
            return True
        else:
            print("❌ 应该拒绝无效数据但没有拒绝")
            return False
            
    except Exception as e:
        print(f"❌ 请求出错: {e}")
        return False

def main():
    print("开始测试钻机配置API接口...\n")
    
    # 先测试写入配置
    write_success = test_write_drill_config()
    
    # 测试获取配置
    get_success = test_get_drill_config()
    
    # 测试无效数据处理
    invalid_test_success = test_with_invalid_data()
    
    print("\n" + "="*50)
    print("测试总结:")
    print(f"写入配置测试: {'通过' if write_success else '失败'}")
    print(f"获取配置测试: {'通过' if get_success else '失败'}")
    print(f"无效数据测试: {'通过' if invalid_test_success else '失败'}")
    
    if write_success and get_success and invalid_test_success:
        print("🎉 所有测试通过!")
    else:
        print("⚠️  部分测试失败，请检查接口实现")

if __name__ == "__main__":
    main()